package net.ojbk.study.curriculumDesign.web.handlers.impl.canvasmodel;

import net.ojbk.study.curriculumDesign.beans.APIResult;
import net.ojbk.study.curriculumDesign.beans.canvasmodel.Category;
import net.ojbk.study.curriculumDesign.common.api.APICodeConstant;
import net.ojbk.study.curriculumDesign.service.impl.canvasmodel.CategoryServiceImpl;
import net.ojbk.study.curriculumDesign.web.handlers.canvasmodel.ICategoryController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("category")
public class CategoryControllerImpl implements ICategoryController {
    @Autowired
    @Qualifier("categoryService")
    CategoryServiceImpl categoryService;

    @RequestMapping("/add")
    public String Promptadd(){return "back/add_category";}
    @RequestMapping("/index")
    @Override
    public String PromptCglist() {
        return "back/categorylist";
    }

    /**再来一个油画类！
     *
     * @return
     */
    @RequestMapping("/insertCg")
    @ResponseBody
    @Override
    public Object setCategory(String cgName) {
        //System.out.println(cgName);
        Category category=new Category();
        category.setCgName(cgName);
        categoryService.addCategory(category);
        return APIResult.createSuccessMessage(new APICodeConstant(),"insert success");
    }
    /**看看我有啥分类
     *
     * @return
     */
    @RequestMapping("/Cglist")
    @ResponseBody
    @Override
    public Object Categorylist(HttpSession session) {
        List<Category> categories=categoryService.getCategories();
       /* Map<String,Object> map=new HashMap<>();
        map.put("total",categories.size());
        map.put("rows",categories);
      JSONObject jo=JSONObject.parseObject(JSON.toJSONString(map));*/
        System.out.println(categories.size());
        return APIResult.createSuccess(new APICodeConstant(),categories);
    }

    /**删一个油画类
     *
     * @return
     */
    @RequestMapping("/Cgdelete")
    @ResponseBody
    @Override
    public Object delCategory(Integer cgid) {
        if (cgid==null)
            return APIResult.createError(new APICodeConstant(),"del failed");
        //System.out.println(cgid);
        categoryService.deleteById(cgid);
        return APIResult.createSuccessMessage(new APICodeConstant(),"del success");
    }
}
